const db = wx.cloud.database({
  env: 'cloud1-2g6vpgs796710242'
})
const app = getApp()
const _ = db.command

Page({
  data: {
    planet_info:[],
    ment:"",
    arrt:"",
    feedback:"使用说明：三元组检索只需要填写实体名称，属性值检索则需要两组都填，否则无法检索。现只支持关键词精确检索。",
    storage:[]
  },

  onLoad: function (options) {
    var user = wx.getStorage({
      key:'user',
      success: (res)=> {
        console.log("查询用户信息缓存",res.data)
        this.setData({
          storage:res.data
        })
      }
    })
    if (app.globalData.ulevel<2) {
      wx.showToast({
        title: '您当前的账号等级不足，无法体验该功能',
        icon:'none',
        duration: 3000
      })
    }
    var id = options.id
    db.collection('cp_list')
    .doc(id)
    .get()
    .then(res=>{
      console.log(res)
      this.setData({
        planet_info:res.data
      })
      //CTR自增
      db.collection("cp_list")
      .doc(id)
      .update({
        data:{
          CTR:_.inc(1)
        }
      })
      .then(res=>{
        console.log("CTR自增成功",res)
      })
      if (!this.data.storage.name=="") {
        console.log('用户信息已授权',user)
        this.setData({
          hasUserInfo: true,
          userInfo: user,
        })
        console.log("用户信息", this.data.userInfo)
        console.log("全局openid",app.globalData.openid)

        db.collection("User")
        .where({
          _openid:app.globalData.openid
        })
        .get()
        .then(res=>{
          console.log("用户信息",res)
          var aimcp = this.data.planet_info.title
          for (var i = 0; i < res.data[0].common.length; i++) {
            if (res.data[0].common[i].name==aimcp) {
              db.collection("User")
              .where({
                _openid:app.globalData.openid,
                'common.name':aimcp
              })
              .update({
                data:{
                  "common.$.count":res.data[0].common[i].count+1
                }
              })
              .then(res=>{
                console.log("使用频次更新成功",res)
              })
              .catch(err=>{
                console.log(err)
              })
            }
          }
        })
      } else {
        console.log("无缓存用户信息")
      }
    })
    .catch(err=>{
      console.log("cp_list云数据库",err)
    })
  },

  display_ment(event) {
    var key = event.detail.value
    this.setData({
      ment: key
    })
    console.log(this.data.ment)
  },

  display_attr(event) {
    var key = event.detail.value
    this.setData({
      arrt: key
    })
    console.log(this.data.arrt)
  },

  search1() {
    var that = this
    if (that.data.ment=="" || that.data.ment==null) {
      wx.showToast({
        title: '请完整填写实体名称',
        duration: 3000,
        icon: 'none'
      })
    } else {
      wx.request({
        url: 'http://shuyantech.com/api/cndbpedia/avpair?q='+that.data.ment,
        method: 'GET',
        success:function(res){
          console.log("三元组结果",res)
          if (res.data.ret=="" || res.data.ret==null) {
            that.setData({
              feedback:"无检索结果，请检查实体词/属性词是否填写完整！"
            })
          } else {
            var fb1 = res.data.ret
            var fbsum = ""
            fb1.forEach(element=>{
              var fblist = element[0]+":"+element[1]+"\n"
              fbsum = fbsum+fblist
            })
            that.setData({
              feedback:fbsum+"[共计"+fb1.length+"条返回数据]"
            })
          }
        },
        fail:function(err) {
          console.log(err)
        }
      })
    }
  },

  search2() {
    var that = this;
    if(that.data.ment=="" || that.data.ment==null || that.data.arrt=="" || that.data.arrt==null) {
      wx.showToast({
        title: '请完整填写实体名称与属性名称',
        duration: 3000,
        icon: 'none'
      })
    } else {
      wx.request({
        url: 'http://shuyantech.com/api/cndbpedia/value?q='+that.data.ment+'&attr='+that.data.arrt,
        method: 'GET',
        success:function(res){
          console.log("属性值结果",res)
          if (res.data.ret=="" || res.data.ret==null) {
            that.setData({
              feedback:"无检索结果，请检查实体词/属性词是否填写完整！"
            })
          } else {
            that.setData({
              feedback:res.data.ret
            })
          }
        },
        fail:function(err) {
          console.log(err)
        }
      })
    }
  }
})